import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../home_children/keep_live_widget.dart';

///
/// 那时我们有梦
/// 关于文学
/// 关于爱情
/// 关于穿越世界的旅行
/// 如今我们深夜饮酒
/// 杯子碰到一起
/// 都是梦破碎的声音
///
class AdaptivePage extends StatefulWidget {
  final String title;

  const AdaptivePage({super.key, required this.title});

  @override
  State<AdaptivePage> createState() => _AdaptivePageState();
}

class _AdaptivePageState extends State<AdaptivePage> {
  int curPageIndex = 0;

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (ctx, con) {
      final height = con.maxHeight;
      final width = con.maxWidth;
      return width > 1.5 * height // 左右结构布局
          ? Scaffold(
              body: Row(
                children: [
                  NavigationRail(  // 左侧导航
                    destinations: [
                      NavigationRailDestination(
                          icon: Icon(Icons.home), label: Text("第一个布局")),
                      NavigationRailDestination(
                          icon: Icon(Icons.business), label: Text("第二个布局"))
                    ],
                    selectedIndex: curPageIndex,
                    onDestinationSelected: (index) {
                      setState(() {
                        curPageIndex = index;
                      });
                    },
                  ),
                  Expanded(
                      child: Column(
                    children: [
                      AppBar(
                        toolbarHeight: kToolbarHeight,
                        title: Text(widget.title),
                      ),
                      Expanded(
                        child: IndexedStack(
                          index: curPageIndex,
                          children: [
                            KeepLiveWidget(),
                            KeepLiveWidget(),
                          ],
                        ),
                      ),
                    ],
                  ))
                ],
              ),
            )
          : Scaffold(
              appBar: AppBar(
                title: Text(widget.title),
              ),
              body: IndexedStack(
                index: curPageIndex,
                children: [
                  KeepLiveWidget(),
                  KeepLiveWidget(),
                ],
              ),
              bottomNavigationBar: BottomNavigationBar(
                items: [
                  BottomNavigationBarItem(
                    icon: Icon(Icons.home),
                    label: '第一个布局',
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.business),
                    label: '第二个布局',
                  )
                ],
                currentIndex: curPageIndex,
                onTap: (index) {
                  setState(() {
                    curPageIndex = index;
                  });
                },
              ),
            );
    });
  }
}
